using System;
using System.Linq;
using ITS.DAL.Members;
using ITS.Core.Domain.Members;
using ITS.DAL.Concrete.EF;

namespace ITS.DAL.Concrete.Members
{
    public class EFMemberRepository : IMemberRepository
    {
        private readonly EnterpriseContext context = new EnterpriseContext();

        public IQueryable<Member> GetAllActiveMembers()
        {
            return context.Members.Where(x => x.Active == true);
        }

        public IQueryable<Member> GetActiveMembersByCategory(int CategoryID)
        {
            return context.Members.Where(x => x.CategoryID == CategoryID && x.Active == true);
        }

        public IQueryable<MemberCategory> GetActiveMemberCategories()
        {
            return context.MemberCategories.Where(x => x.Active == true);
        }


        public void Save(Member o)
        {
            if (o.MemberID.Equals(Guid.Empty))
            {
                o.MemberID = Guid.NewGuid();
                context.Members.Add(o);
            }
            else
            {
                context.Entry(o).State = System.Data.EntityState.Modified;
            }
            context.SaveChanges();
        }


        public Member Get(Guid memberID)
        {
            return context.Members.FirstOrDefault(x => x.MemberID == memberID);
        }
    }
}
